package svg.sample;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import com.neural.*;

public class QuizCanvas extends JPanel {

    private Entry entry;

    /**
     * The image that the user is drawing into.
     */
    protected Image entryImage;

    /**
     * A graphics handle to the image that the
     * user is drawing into.
     */
    protected Graphics entryGraphics;


    private int lastX, lastY;//x1, y1, x, y;
    private boolean mouseButtonDown = false;


    public QuizCanvas() {
        entry = new Entry();

        setBackground(Color.white);

        //add mouse (e.g press, release) event handler
        addMouseListener(new MouseHandler());

        //add mouse motion (e.g mouse drag) event handler
        addMouseMotionListener(new MouseMotionHandler());
    }


    /**
     * Setup the internal image that the user
     * draws onto.
     */
    protected void initImage() {
        entryImage = createImage(getWidth(), getHeight());
        entryGraphics = entryImage.getGraphics();
        entryGraphics.setColor(Color.white);
        entryGraphics.fillRect(0, 0, getWidth(), getHeight());
    }

    protected void clear() {
        this.entryGraphics.setColor(Color.white);
        this.entryGraphics.fillRect(0, 0, getWidth(), getHeight());
        repaint();
    }

    /**
     * Paint the drawn image and cropping box
     * (if active).
     *
     * @param g The graphics context
     */
    public void paint(Graphics g) {
        if (entryImage == null) {
            initImage();
        }
        g.drawImage(entryImage, 0, 0, this);
        g.setColor(Color.black);
        g.drawRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.red);
        /*g.drawRect(downSampleLeft,
                   downSampleTop,
                   downSampleRight - downSampleLeft,
                   downSampleBottom - downSampleTop);*/
    }



    public SampleData getSampleData() {
        return entry.downSample("", entryImage);
    }

    //MouseEventListeners: for creating points--added
    //wonder if any of these are ever needed...right now lets just leave it
    //look into later--guess they are needed...to draw on canvas :)
    class MouseHandler implements MouseListener {
        public void mouseClicked(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {
            lastX = e.getX();
            lastY = e.getY();
            mouseButtonDown = true;
        }

        public void mouseReleased(MouseEvent e) {
            mouseButtonDown = false;
        }
    }

    class MouseMotionHandler implements MouseMotionListener {
        public void mouseDragged(MouseEvent e) {
            if (mouseButtonDown) {
                entryGraphics.setColor(Color.black);
                entryGraphics.drawLine(lastX, lastY, e.getX(), e.getY());
                getGraphics().drawImage(entryImage, 0, 0, QuizCanvas.this);
                lastX = e.getX();
                lastY = e.getY();
            }
        }

        public void mouseMoved(MouseEvent e) {}
    }


}
